package country;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;

import company.Company;

import city.City;
import descriptor.CitiesListDescriptor;
import descriptor.CityDescriptor;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CountryDescriptor;

/**
 * CountryBean
 * 
 * 
 * The CountryBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Country).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class CountryBean implements EntityBean 
{	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public abstract String getName();
	public abstract void setName(String name);
	
	public abstract Collection<City> getCities();
	public abstract void setCities(Collection<City> cities);
	
	public abstract Collection<Company> getCompanies();
	public abstract void setCompanies(Collection<Company> companies);
	
	 public String ejbCreate(String name) throws CreateException
	 {  	
	    	setName(name);
	    	setCities(new Vector<City>());
	    	setCompanies(new Vector<Company>());
	    	
			return null;
	 }
	 
	 public void ejbPostCreate(String name) throws CreateException{}
	 public void ejbRemove() throws EJBException, RemoteException, RemoveException{}
	 public void ejbActivate() throws EJBException, RemoteException {}
	 public void ejbPassivate() throws EJBException, RemoteException {}
	 public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	 public void ejbLoad() throws EJBException, RemoteException {}
	 public void ejbStore() throws EJBException, RemoteException {}
	 public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	 public void unsetEntityContext() throws EJBException, RemoteException {}
	 
	 public CountryDescriptor getDescriptor()
	 {
		 return new CountryDescriptor(getId(),getName(),getCitiesListDescriptor(),getCompaniesListDescriptor());
	 }
	 
	 public CitiesListDescriptor getCitiesListDescriptor()
	 {
		Collection<City> cityColl = getCities();
		CitiesListDescriptor desc = new CitiesListDescriptor();
		for(City city : cityColl)
		{
			desc.addCityDescriptor(new CityDescriptor(city.getId(),city.getName(),null,this.getId(),null));
		}
		return desc;
	}
	 
	 public CompaniesListDescriptor getCompaniesListDescriptor()
	 {
		Collection<Company> compColl = getCompanies();
		CompaniesListDescriptor desc = new CompaniesListDescriptor();
		for(Company comp : compColl)
		{
			desc.addCompanyDescriptor(new CompanyDescriptor(comp.getId(),comp.getName(),comp.getAddress(),comp.getPhone(),comp.getEmail(),comp.getCity().getName(),null,comp.getSector().getName(),comp.getOffers().size(),null,null,null));
		}
		return desc;
	}
}
